package com.micangcaihang.micang.mvp.task.presenter;

import com.micangcaihang.common.baserx.RxSchedulers;
import com.micangcaihang.common.http.ApiResult;
import com.micangcaihang.common.http.RxHelper;
import com.micangcaihang.common.http.exception.ApiException;
import com.micangcaihang.micang.entity.TaskPriceSurveyEntity;
import com.micangcaihang.micang.http.MiSubScriber;
import com.micangcaihang.micang.mvp.task.contract.TaskPriceSurveyContract;
import com.micangcaihang.micang.mvp.task.model.TaskPriceSurveyModel;

import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.annotations.NonNull;
import io.reactivex.functions.Function;

/**
 * <pre>
 *     author : Fandy
 *     e-mail : fandayongfly@gmail.com
 *     time   : 2017/07/17
 *     desc   : 价格调查P
 *     version: 1.0
 * </pre>
 */

public class TaskPriceSurveyPresenter extends TaskPriceSurveyContract.Presenter {
    private TaskPriceSurveyModel mModel;
    private String id;

    @Override
    public void setV(TaskPriceSurveyContract.View v) {
        super.setV(v);
        mModel = new TaskPriceSurveyModel();
    }

    public void setId(String id) {
        this.id = id;
    }

    /**
     * 每次进入界面获取网络数据，更新界面
     *
     * @param isRefresh 新增参数，用来判断是否为下拉刷新调用，下拉刷新的时候不应该再显示加载界面和异常界面
     */
    @Override
    public void loadFirstData(boolean isRefresh) {
        loadPriceSurveyData();
    }

    /**
     * 加载更多数据
     */
    @Override
    public void loadMoreData() {

    }

    /**
     * 加载价格调查数据
     */
    @Override
    public void loadPriceSurveyData() {
        Observable<ApiResult<TaskPriceSurveyEntity>> observable = mModel.getPriceSurveyData(id);
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<TaskPriceSurveyEntity>>() {

            @Override
            protected void doOnStart() {
                mView.showLoading();
            }

            @Override
            protected void doOnNext(ApiResult<TaskPriceSurveyEntity> result) {
                if (result.data != null) {
                    setPriceSurveyData(result);
                    mView.returnPriceSurveyData(result.data);
                }
            }

            @Override
            protected void doOnComplete() {
                mView.showContent();
            }

            @Override
            protected void doOnError(ApiException e) {
//                super.doOnError(e);
                mView.showNetError(e);
            }
        }));
    }

    private void setPriceSurveyData(ApiResult<TaskPriceSurveyEntity> result) {
        List<TaskPriceSurveyEntity.XinLou> xinLous = result.data.getXinlou();
        List<TaskPriceSurveyEntity.ZhongJie> zhongJies = result.data.getZhongjie();
        if (xinLous == null || xinLous.size() <= 0) {
            TaskPriceSurveyEntity.XinLou bean = new TaskPriceSurveyEntity.XinLou();
            bean.setEstimate_fee("");
            bean.setReference_name("");
            bean.setUnit_price("");
            bean.setEvaluate_id("0");
            xinLous.add(bean);
        }
        if (zhongJies == null || zhongJies.size() <= 0) {
            TaskPriceSurveyEntity.ZhongJie bean = new TaskPriceSurveyEntity.ZhongJie();
            bean.setContacts("");
            bean.setEvaluate_source("");
            bean.setReference_name("");
            bean.setUnit_price("");
            bean.setEvaluate_id("0");
            zhongJies.add(bean);
        }
    }

    /**
     * 保存信息
     *
     * @param request
     */
    @Override
    public void savePriceSurveyData(TaskPriceSurveyEntity request) {
        Observable<ApiResult<TaskPriceSurveyEntity>> observable3 = mModel.getPriceSurveyData(id);
        List<TaskPriceSurveyEntity.XinLou> xinlou = request.getXinlou();
        List<TaskPriceSurveyEntity.ZhongJie> zhongjie = request.getZhongjie();
        if (xinlou.size() > 0 && zhongjie.size() > 0) {
            Observable<ApiResult<Void>> observable = mModel.savaXinlouData(request);
            Observable<ApiResult<Void>> observable1 = mModel.savaZhongJieData(request);
            mRxManage.add(observable.concatMap(new Function<ApiResult<Void>, ObservableSource<ApiResult<Void>>>() {
                @Override
                public ObservableSource<ApiResult<Void>> apply(@NonNull ApiResult<Void> voidApiResult) throws Exception {
                    return observable1;
                }
            }).compose(RxSchedulers.observableIoToMain())
                    .compose(RxHelper.handleResult())
                    .concatMap(new Function<ApiResult<Void>, ObservableSource<ApiResult<TaskPriceSurveyEntity>>>() {
                        @Override
                        public ObservableSource<ApiResult<TaskPriceSurveyEntity>> apply(@NonNull ApiResult<Void> voidApiResult) throws Exception {
                            return observable3;
                        }
                    }).compose(RxSchedulers.observableIoToMain())
                    .compose(RxHelper.handleResult())
                    .subscribeWith(new MiSubScriber<ApiResult<TaskPriceSurveyEntity>>() {

                        @Override
                        protected void doOnStart() {
                            mView.showDialogLoading("");
                        }

                        @Override
                        protected void doOnNext(ApiResult<TaskPriceSurveyEntity> result) {
                            if (result.data != null) {
                                setPriceSurveyData(result);
                                mView.returnPriceSurveyData(result.data);
                                mView.showTip("保存成功");
                            }
                        }

                        @Override
                        protected void doOnComplete() {
                            mView.hideDialogLoading();
                        }

                        @Override
                        protected void doOnError(ApiException e) {
                            super.doOnError(e);
                            mView.hideDialogLoading();
                        }
                    }));
        } else if (xinlou.size() > 0 && zhongjie.size() < 0) {
            Observable<ApiResult<Void>> observable = mModel.savaXinlouData(request);
            mRxManage.add(observable.concatMap(new Function<ApiResult<Void>, ObservableSource<ApiResult<TaskPriceSurveyEntity>>>() {
                @Override
                public ObservableSource<ApiResult<TaskPriceSurveyEntity>> apply(@NonNull ApiResult<Void> voidApiResult) throws Exception {
                    return observable3;
                }
            }).compose(RxSchedulers.observableIoToMain())
                    .compose(RxHelper.handleResult())
                    .subscribeWith(new MiSubScriber<ApiResult<TaskPriceSurveyEntity>>() {

                        @Override
                        protected void doOnStart() {
                            mView.showDialogLoading("");
                        }

                        @Override
                        protected void doOnNext(ApiResult<TaskPriceSurveyEntity> result) {
                            if (result.data != null) {
                                setPriceSurveyData(result);
                                mView.returnPriceSurveyData(result.data);
                                mView.showTip("保存成功");
                            }
                        }

                        @Override
                        protected void doOnComplete() {
                            mView.hideDialogLoading();
                        }

                        @Override
                        protected void doOnError(ApiException e) {
                            super.doOnError(e);
                            mView.hideDialogLoading();
                        }
                    }));
        } else if (xinlou.size() < 0 && zhongjie.size() > 0) {
            Observable<ApiResult<Void>> observable = mModel.savaZhongJieData(request);
            mRxManage.add(observable.concatMap(new Function<ApiResult<Void>, ObservableSource<ApiResult<TaskPriceSurveyEntity>>>() {
                @Override
                public ObservableSource<ApiResult<TaskPriceSurveyEntity>> apply(@NonNull ApiResult<Void> voidApiResult) throws Exception {
                    return observable3;
                }
            }).compose(RxSchedulers.observableIoToMain())
                    .compose(RxHelper.handleResult())
                    .subscribeWith(new MiSubScriber<ApiResult<TaskPriceSurveyEntity>>() {

                        @Override
                        protected void doOnStart() {
                            mView.showDialogLoading("");
                        }

                        @Override
                        protected void doOnNext(ApiResult<TaskPriceSurveyEntity> result) {
                            if (result.data != null) {
                                setPriceSurveyData(result);
                                mView.returnPriceSurveyData(result.data);
                                mView.showTip("保存成功");
                            }
                        }

                        @Override
                        protected void doOnComplete() {
                            mView.hideDialogLoading();
                        }

                        @Override
                        protected void doOnError(ApiException e) {
                            super.doOnError(e);
                            mView.hideDialogLoading();
                        }
                    }));
        } else {
            mView.showTip("填写价格之后继续保存");
        }

    }

    /**
     * 删除价格调查
     *
     * @param id
     * @param evaluate_id
     */
    @Override
    public void deletePriceData(String id, String evaluate_id) {
        Observable<ApiResult<Void>> observable = mModel.deletePriceData(id, evaluate_id);
        mRxManage.add(observable.subscribeWith(new MiSubScriber<ApiResult<Void>>() {
            @Override
            protected void doOnStart() {
                mView.showDialogLoading("");
            }

            @Override
            protected void doOnNext(ApiResult<Void> voidApiResult) {
                mView.deletePriceSuccess();
                mView.showTip("删除成功");
            }

            @Override
            protected void doOnComplete() {
                mView.hideDialogLoading();
            }

            @Override
            protected void doOnError(ApiException e) {
                super.doOnError(e);
                mView.hideDialogLoading();
            }
        }));
    }
}
